<?php

namespace App\Http\Controllers;
// use Illuminate\Http\Request;
use Illuminate\Http\Request;
// use Request;
use Illuminate\Support\Facades\Hash;
use Session;

use App\Models\GeoJson;
use App\Models\DangAn;
use App\Models\Users;

class DangAnController extends Controller
{
    // 要素编辑
    // function editfeatures()
    // {
    //   $geojsons = GeoJson::orderBy('add_time', 'desc')->paginate(20);
    //   return view('dangan/editfeatures', array('geojsons' => $geojsons));
    // }

  // 登录页面
  function login()
  {
    return view('dangan/login');
  }

  // 登录验证
  function login_post(Request $request)
  {
    $username = $request->input('username');
    $password = $request->input('password');

    $user = Users::where('user_name', $username)->first();

    if(empty($user))
    {
        return '账号错误';
    }

    if(Hash::check($password, $user->password)) {
        // 密码正确
        // echo '密码正确';
        Session::put('username', $username); // 管理员名字
        Session::put('userid', $user->id); // 管理员id
        return redirect('dangan/index');
    } else {
        // 密码错误
        return '密码错误';
    }
  }

  // 前台退出登录
  function logout()
  {
      Session::forget('username');
      Session::forget('userid');
      return redirect('dangan/login');
  }

  // 首页展示
  function index()
  {
    $dangans = DangAn::paginate(30);
    $geojsons = GeoJson::where('show', 1)->get();
    return view('dangan/index', array(
      'dangans' => $dangans,
      'geojsons' => $geojsons
    ));
  }

  // 显示所有的档案列表
  // function danganall()
  // {
  //   $dangans = DangAn::paginate(30);
  //   return view('dangan/danganall', array(
  //     'dangans' => $dangans
  //   ));
  // }

  // 按关键字搜索
  function search(Request $request)
  {
    $key = $request->input('key');
    $dangans = DangAn::where('danganhao', 'like', '%'.trim($key).'%')
                      ->OrWhere('xiangmumingcheng', 'like', '%'.trim($key).'%')
                      ->OrWhere('jianshedanwei', 'like', '%'.trim($key).'%')
                      ->OrWhere('xuanzhizheng', 'like', '%'.trim($key).'%')
                      ->OrWhere('yongdizheng', 'like', '%'.trim($key).'%')
                      ->OrWhere('fanganshendingtongzhishu', 'like', '%'.trim($key).'%')
                      ->OrWhere('gongchengzheng', 'like', '%'.trim($key).'%')
                      ->OrWhere('jungongzheng', 'like', '%'.trim($key).'%')
                      ->OrWhere('tiaojianbianhao', 'like', '%'.trim($key).'%')
                      ->OrWhere('xiangcunbianhao', 'like', '%'.trim($key).'%')
                      ->orderBy('id', 'asc')->paginate(30);
    return view('dangan/danganall', array(
      'dangans' => $dangans,
      'key' => $key
    ));
  }

}

